<script setup lang="ts">
import { useTraining } from "../utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import View from "@iconify-icons/ep/view";
import UserFilled from "@iconify-icons/ep/user-filled";

defineOptions({
  name: "TrainingList"
});

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  buttonClass,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleDetail,
  handleEnroll,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = useTraining();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="培训标题：" prop="title">
        <el-input
          v-model="form.title"
          placeholder="请输入培训标题"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="培训分类：" prop="category">
        <el-select
          v-model="form.category"
          placeholder="请选择培训分类"
          clearable
          class="!w-[140px]"
        >
          <el-option label="羽毛球培训" value="badminton" />
          <el-option label="乒乓球培训" value="table-tennis" />
          <el-option label="网球培训" value="tennis" />
          <el-option label="篮球培训" value="basketball" />
          <el-option label="足球培训" value="football" />
          <el-option label="游泳培训" value="swimming" />
          <el-option label="健身培训" value="fitness" />
          <el-option label="瑜伽培训" value="yoga" />
          <el-option label="其他培训" value="other" />
        </el-select>
      </el-form-item>
      <el-form-item label="讲师姓名：" prop="instructorName">
        <el-input
          v-model="form.instructorName"
          placeholder="请输入讲师姓名"
          clearable
          class="!w-[120px]"
        />
      </el-form-item>
      <el-form-item label="培训状态：" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择培训状态"
          clearable
          class="!w-[120px]"
        >
          <el-option label="草稿" value="0" />
          <el-option label="已发布" value="1" />
          <el-option label="进行中" value="2" />
          <el-option label="已完成" value="3" />
          <el-option label="已取消" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="培训级别：" prop="level">
        <el-select
          v-model="form.level"
          placeholder="请选择培训级别"
          clearable
          class="!w-[120px]"
        >
          <el-option label="初级" value="1" />
          <el-option label="中级" value="2" />
          <el-option label="高级" value="3" />
          <el-option label="专家级" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="培训时间：">
        <el-date-picker
          v-model="form.startTime"
          type="date"
          placeholder="开始日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          class="!w-[120px]"
        />
        <span class="mx-2">至</span>
        <el-date-picker
          v-model="form.endTime"
          type="date"
          placeholder="结束日期"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          class="!w-[120px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar title="培训列表" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog()"
        >
          新增培训
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(View)"
              @click="handleDetail(row)"
            >
              查看详情
            </el-button>
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('编辑', row)"
            >
              编辑
            </el-button>
            <el-button
              v-if="row.status === 1 && row.currentStudents < row.maxStudents"
              class="reset-margin"
              link
              type="success"
              :size="size"
              :icon="useRenderIcon(UserFilled)"
              @click="handleEnroll(row)"
            >
              报名
            </el-button>
            <el-popconfirm
              :title="`是否确认删除培训${row.title}的这条数据`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>